<?php

namespace app\api\controller;

use support\Request;
use app\ai\model\User;
use app\ai\model\Task;

class UserController
{

    /**
     * 获取用户信息
     */
    public function userInfo(Request $request){
        $uid = $request->userData['user_id'];
        $userInfo = User::with('level')->find($uid);
        return json(['code'=>200,'msg'=>'success','data'=>$userInfo]);
    }


    /**
     * 修改用户信息
     */
    public function profile(Request $request)
    {
        $avatar = $request->post('avatar');
        $nickname = $request->post('nickname');
        if (!$avatar && !$nickname) {
            return json(['code' => 400, 'msg' => '参数错误']);
        }
        $data = [];
        if ($avatar) {
            $data['avatar'] = $avatar;
        }
        if ($nickname) {
            $data['nickname'] = $nickname;
        }
        $uid = $request->userData['user_id'];
        $user = User::find($uid);
        if (!$user) {
            return json(['code' => 400, 'msg' => '用户不存在']);
        }
        $user->save($data);
        return json(['code' => 200, 'msg' => '更新个人信息成功','data' => $user]);  
    }


    /**
     * 获取用户的任务队列
     */
    public function task(Request $request){
        $page = (int)$request->input('page', 1);
        $limit = (int)$request->input('limit', 10);
        $uid = $request->userData['user_id'];
        $list = Task::alias('t')
        ->where('t.uid', $uid)
        ->where('t.task_status','!=','3')
        ->join('ai_template tp', 'tp.id = t.template_id')
        ->join('ai_template_price p', 'p.template_id = t.template_id AND p.sort = t.articulation')
        ->field([
            't.id',
            't.original_img_url',
            't.result_img_url',
            't.template_id',
            't.task_status',
            // 't.articulation',
            'tp.name as template_name',
            'p.name as price_name'
        ])
        ->order('t.create_time', 'desc')
        ->paginate([
            'list_rows' => $limit,
            'page' => $page,
        ]);

        return json([
            'code' => 200,
            'msg' => 'success',
            'data' => [
                'list' => $list->items(),
                'total' => $list->total(),
                'last_page' => $list->lastPage(),
                'current_page' => $list->currentPage(),
            ]
        ]);
    }

    /**
     * 获取用户已经完成的任务相册
     */
    public function photo(Request $request){
        $page = (int)$request->input('page', 1);
        $limit = (int)$request->input('limit', 10);
        $uid = $request->userData['user_id'];
        $list = Task::alias('t')
        ->where('t.uid', $uid)
        ->where('t.task_status','=','3')
        ->join('ai_template tp', 'tp.id = t.template_id')    
        ->join('ai_template_price p', 'p.template_id = t.template_id AND p.sort = t.articulation')
        ->field([
            't.id',
            't.task_no',
            't.original_img_url',
            't.result_img_url',
            't.is_share',
            't.view',
            't.template_id',
            't.task_status',
            't.articulation',
            't.check_status',
            'tp.name as template_name',
            'p.name as price_name'
        ])
        ->order('t.create_time', 'desc')
        ->paginate([
            'list_rows' => $limit,
            'page' => $page,
        ]);

        return json([
            'code' => 200,
            'msg' => 'success',
            'data' => [
                'list' => $list->items(),
                'total' => $list->total(),
                'last_page' => $list->lastPage(),
                'current_page' => $list->currentPage(),
            ]
        ]);
    }

    /**
     * 点赞接口
     */
    public function like(Request $request){
        $id = $request->post('id');
        $task = Task::find($id);
        if (!$task) {
            return json(['code' => 400, 'msg' => '任务不存在']);
        }
        $task->view = $task->view += 1;
        $task->save();
        return json(['code' => 200, 'msg' => '点赞成功','data'=>$task->view]);
    }
}
